/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __get_ejb_name.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/11 by jikw                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include <corba/corba_comm.h>
#include <corba/corba_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "frlist.h"
#include "frreport_comm.h"
#include "apmtest_cunit.h"
#include "apmtest_comm.h"

#include "../../../src/module/corba/config.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __get_ejb_name(corba_session *p_st_session_private
                           , Frreport_body_data *p_st_body_data);

extern L7session_corba_private* __corba_private_create(void);
extern corba_session* __corba_session_create(void);

static void case1(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case2(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    corba_session_destroy(p_st_session_private);

    return;
}

static void case3(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_key_address = (char *)frmalloc(10);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    corba_session_destroy(p_st_session_private);

    return;
}

static void case4(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_key_address = (char *)frmalloc(10);
    p_st_session_private->p_st_private = __corba_private_create();

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    corba_session_destroy(p_st_session_private);

    return;
}

static void case5(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_data = (char *)"A";
    int i_data_len = strlen(p_data);
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_st_private = __corba_private_create();
    p_st_session_private->p_key_address = (char *)frmalloc(i_data_len+1);
    memcpy(p_st_session_private->p_key_address, p_data, i_data_len);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_EQUAL(p_st_session_private->p_st_private->p_title, NULL);

    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    corba_session_destroy(p_st_session_private);

    return;
}

static void case6(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_data = (char *)"\x4a\x4d\x42\x49\x00\x00\x00\x10\x47\x73\xe3\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\x08\x00\x00\x00\x01\x00";
    int i_data_len = 41;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_st_private = __corba_private_create();
    p_st_session_private->p_key_address = (char *)frmalloc(i_data_len+1);
    memcpy(p_st_session_private->p_key_address, p_data, i_data_len);
    p_st_session_private->i_key_addr_len = 41;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_EQUAL(p_st_session_private->p_st_private->p_title, NULL);

    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    corba_session_destroy(p_st_session_private);

    return;
}

static void case7(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_data = (char *)"\x4a\x4d\x42\x49\x00\x00\x00\x10\x47\x73\xe3\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\x08\x00\x00\x00\x01\x00\x00\x00\x00";
    int i_data_len = 44;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_st_private = __corba_private_create();
    p_st_session_private->p_key_address = (char *)frmalloc(i_data_len+1);
    memcpy(p_st_session_private->p_key_address, p_data, i_data_len);
    p_st_session_private->i_key_addr_len = 44;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_EQUAL(p_st_session_private->p_st_private->p_title, NULL);

    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    corba_session_destroy(p_st_session_private);

    return;
}

static void case8(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_data = (char *)"\x4a\x4d\x42\x49\x00\x00\x00\x12\x47\x73\xe3\xaa\x37\x64\x30\x62\x63\x37\x37\x33\x65\x33\x61\x61\x66\x63\x33\x34\x00\x00\x00\x24\x00\x00\x00\x6d\x49\x45\x4a\x50\x02\x00\x3d\x8c\xfc\x93\x07\x73\x65\x72\x76\x65\x72\x31\x03\x45\x4a\x42\x00\x00\x00\x53\xac\xac\x00\x02\x00\x01\x00\x29\x00\x00\x00\x5f\x5f\x68\x6f\x6d\x65\x4f\x66\x48\x6f\x6d\x65\x73\x23\x5f\x5f\x68\x6f\x6d\x65\x4f\x66\x48\x6f\x6d\x65\x73\x23\x5f\x5f\x68\x6f\x6d\x65\x4f\x66\x48\x6f\x6d\x65\x73\x08\x45\x4a\x42\x53\x79\x73\x74\x65\x6d\x23\x45\x4a\x42\x2e\x6a\x61\x72\x23\x48\x65\x6c\x6c\x6f\x53\x65\x73\x73\x69\x6f\x6e";
    int i_data_len = 145;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_st_private = __corba_private_create();
    p_st_session_private->p_key_address = (char *)frmalloc(i_data_len+1);
    memset(p_st_session_private->p_key_address, 0x00, i_data_len+1);
    memcpy(p_st_session_private->p_key_address, p_data, i_data_len);
    p_st_session_private->i_key_addr_len = 145;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_EQUAL(p_st_session_private->p_st_private->p_title, NULL);

    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    corba_session_destroy(p_st_session_private);

    return;
}

static void case9(void)
{
    corba_session *p_st_session_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    char *p_data = (char *)"\x4a\x4d\x42\x49\x00\x00\x00\x12\x47\x73\xe3\xaa\x37\x64\x30\x62\x63\x37\x37\x33\x65\x33\x61\x61\x66\x63\x33\x34\x00\x00\x00\x24\x00\x00\x00\x43\x49\x45\x4a\x50\x02\x00\xcb\xb2\xfc\xc0\x07\x73\x65\x72\x76\x65\x72\x31\x03\x45\x4a\x42\x00\x00\x00\x29\xac\xac\x00\x02\x00\x01\x11\x1e\x00\x00\x00\x45\x4a\x42\x53\x79\x73\x74\x65\x6d\x23\x45\x4a\x42\x2e\x6a\x61\x72\x23\x48\x65\x6c\x6c\x6f\x53\x65\x73\x73\x69\x6f\x6e";
    int i_data_len = 103;
    int i_ret = 0;

    p_st_session_private = __corba_session_create();
    p_st_session_private->p_st_private = __corba_private_create();
    p_st_session_private->p_key_address = (char *)frmalloc(i_data_len+1);
    memset(p_st_session_private->p_key_address, 0x00, i_data_len+1);
    memcpy(p_st_session_private->p_key_address, p_data, i_data_len);
    p_st_session_private->i_key_addr_len = 103;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    i_ret = __get_ejb_name(p_st_session_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_session_private->p_st_private->p_title, "EJBSystem");

    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    corba_session_destroy(p_st_session_private);

    return;
}

int add_suite__get_ejb_name(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __get_ejb_name()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);
    CU_add_test(pSuite, "case7", case7);
    CU_add_test(pSuite, "case8", case8);
    CU_add_test(pSuite, "case9", case9);

    return FR_SUCC;

}


